package co.fitstart.coach.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.coach.entity.security.Login;
import co.fitstart.coach.service.CourseService;
import co.fitstart.coach.service.GradeService;
import co.fitstart.coach.service.PrincipalService;
import co.fitstart.coach.web.resolver.Secure;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;

/**
 * 课时评分
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("")
public class GradeController {

    private static final Logger logger = LoggerFactory.getLogger(GradeController.class);

    @Autowired
    private PrincipalService principalService;

    @Autowired
    private GradeService gradeService;

    @Autowired CourseService courseService;
    /**
     * 未评分课时列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/ungraded-schedules", method = RequestMethod.GET)
    public ModelAndView listUngradedSchedule(@Secure Login login,
                                             @RequestParam(name = "grade", required = false, defaultValue="false") boolean grade) {
        ModelAndView modelAndView = new ModelAndView("grade/ungraded-schedule-list");
        
        Principal coach = principalService.load(Principal.class, login.getId());
        
        List<CourseSchedule> courseSchedules;
        
        if(grade) {
            courseSchedules = gradeService.listGradedSchedule(coach);
        } else {
            courseSchedules = gradeService.listUngradedSchedule(coach);
        }
        
        modelAndView.addObject("grade", grade);
        modelAndView.addObject("courseSchedules", courseSchedules);
        
        return modelAndView;
    }

    /**
     * 
     * @param login
     * @param scheduleId
     * @return
     */
    @RequestMapping(value = "/ungraded-schedules/{scheduleId}/grade", method = RequestMethod.GET)
    public ModelAndView grade(@Secure Login login,
                              @PathVariable("scheduleId") Long scheduleId) {
        ModelAndView modelAndView = new ModelAndView("grade/ungraded-schedule-detail");
        
        Principal coach = principalService.load(Principal.class, login.getId());
        
        CourseSchedule courseSchedule = courseService.loadCourseSchedule(scheduleId);
        
        if(courseSchedule.getCoach() == coach) {
            modelAndView.addObject("courseSchedule", courseSchedule);
        }
        
        return modelAndView;
    }

    /**
     * 
     * @param login
     * @param scheduleId
     * @param grade
     * @return
     */
    @RequestMapping(value = "/ungraded-schedules/{scheduleId}/grade", method = RequestMethod.POST)
    public @ResponseBody String onGrade(@Secure Login login,
                                        @PathVariable("scheduleId") Long scheduleId,
                                        @RequestParam("score") int grade){
        
        Result result = new Result();
        
        try {
            logger.info("#onGrade with params coachId [{}], courseScheduleId [{}], grade [{}]", login.getId(), scheduleId, grade);
            
            Principal coach = principalService.load(Principal.class, login.getId());
            
            CourseSchedule courseSchedule = courseService.loadCourseSchedule(scheduleId);
            
            if(coach != courseSchedule.getCoach()) {
                result.fail().message("你不是当前课程上课教练，不能评分");
            } else {
                gradeService.grade(courseSchedule, grade);
                
                result.success()
                      .message("评分成功");
            }
        } catch(Exception e) {
            
            logger.error("#onGrade - ", e);
            
            result.fail().message("评分失败");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
}
